#include "mpCodecUtils.h"
#include "mpCodecUtilsTests.h"


mpCodecUtilsTests::mpCodecUtilsTests()
{
}

void mpCodecUtilsTests::initTestCase(){}
void mpCodecUtilsTests::cleanupTestCase(){}


/**
 * @brief 字符base64编码
 */
void mpCodecUtilsTests::case_base64_encode(){

#ifdef TEST_STRATEGY_COMPLETED

    std::string txt = "我是中国人 ; I am Chinese";
    std::string base64 = "";

    unsigned char * temp = (unsigned char *)txt.c_str();
    int len = strlen((char *)temp) ;//sizeof(text)-1;

    MP::CodecUtils::base64_encode(temp,len,base64);

    QVERIFY2(base64 == "5oiR5piv5Lit5Zu95Lq6IDsgSSBhbSBDaGluZXNl","字符base64编码");

#endif

}

/**
 * @brief 字符base64解码
 */
void mpCodecUtilsTests::case_base64_decode(){

#ifdef TEST_STRATEGY_COMPLETED

    std::string txt = "";
    std::string base64 = "5oiR5piv5Lit5Zu95Lq6IDsgSSBhbSBDaGluZXNl";

    MP::CodecUtils::base64_decode(base64,txt);

    QVERIFY2(txt == "我是中国人 ; I am Chinese","字符base64解码");
#endif
}

/**
 * @brief des 加密
 */
void mpCodecUtilsTests::case_des_ecb_encode(){
#ifdef TEST_STRATEGY_COMPLETED

    std::string key = "mapleleaf0";
    std::string txt = "我是中国人 ; I am Chinese";
    std::string des = "";

    MP::CodecUtils::des_ecb_encode(txt,key,des);


    QVERIFY2(des == "34Z717kg8yceLr5/nfScpmv2xCvDGYDd7/kNR2NVopI=","des 加密");
#endif

}

/**
 * @brief des 解密
 */
void mpCodecUtilsTests::case_des_ecb_decode(){

#ifdef TEST_STRATEGY_COMPLETED

    std::string key = "mapleleaf0";
    std::string txt = "";
    std::string des = "34Z717kg8yceLr5/nfScpmv2xCvDGYDd7/kNR2NVopI=";

    MP::CodecUtils::des_ecb_decode(des,key,txt);


    QVERIFY2(txt == "我是中国人 ; I am Chinese","des 解密");

#endif
}


/**
 * @brief 将UTF8格式的字符串做URL编码
 */
void mpCodecUtilsTests::case_utf8Url_encode(){

#ifdef TEST_STRATEGY_COMPLETED

    //数据源：http://www.ab173.com/enc/urlencode.php
    //函数不会对特殊字符进行编码

    std::string url = "www.hwakun.com.cn/a=1&b=张三&c=王五";
    std::string url_encode = "www.hwakun.com.cn/a=1&b=%E5%BC%A0%E4%B8%89&c=%E7%8E%8B%E4%BA%94";
    std::string txt = "";

    MP::CodecUtils::utf8Url_encode(txt,url);
    QVERIFY2(txt == url_encode,"将UTF8格式的字符串做URL编码");

#endif
}

/**
 * @brief 将UTF8格式的字符串做URL解码
 */
void mpCodecUtilsTests::case_utf8Url_decode(){
#ifdef TEST_STRATEGY_COMPLETED

    std::string url = "www.hwakun.com.cn/a=1&b=张三&c=王五";
    std::string url_encode = "www.hwakun.com.cn/a=1&b=%E5%BC%A0%E4%B8%89&c=%E7%8E%8B%E4%BA%94";
    std::string txt = "";

    MP::CodecUtils::utf8Url_decode(txt,url_encode);
    QVERIFY2(txt == url,"将UTF8格式的字符串做URL解码");

#endif

}
